//
// Created by fazhehy on 2024/3/14.
//

#include "fingerprint.h"

void display_progress(uint8_t progress)
{
    if (progress >= 100){
        oled_clear_buffer();
        oled_show_bmp(32, 0, ok_picture, 64, 64);
        oled_update_screen();
        oled_clear_buffer();
        HAL_Delay(800);
    }
    else{
        oled_show_num(64-12, 32-8-9+5, progress, 3);
        oled_show_char(64+3*6-11, 32-8-9+5, '%');
        oled_draw_round_rect(64-40, 32-7+5, 80, 15, 5);
        if (80*progress/100 > 10)
            oled_draw_fill_round_rect(64-40, 32-7+5, 80*progress/100, 15, 5);
        oled_update_screen();
    }
}

void display_error(char * str)
{
    char temp[22];
    if (strlen(str)<=21)
        oled_show_string(0, 0, str);
    else{
        strncpy(temp, str, 21);
        oled_show_string(0, 0, temp);
        oled_show_string(0, 9, str+21);
    }
    oled_update_screen();
}

void display_finger_num()
{
    uint8_t fingerNum;
    uint8_t state = fpm383c_get_fingerprint_num(&fingerNum);
    while (state != 0x00){
        HAL_Delay(200);
        state = fpm383c_get_fingerprint_num(&fingerNum);
    }
    oled_show_num(12*6, 56, fingerNum, 2);
    oled_show_string(0, 56, "finger num:");
    oled_update_screen();
}

void add_fingerprint()
{
    uint8_t reg_idx = 1;
    uint16_t id = 0;
    uint8_t proc = 0, state = 0;
    display_finger_num();
    display_progress(0);
    for (;;){
        if (fpm383c_register_fingerprint(reg_idx) != 0x00){
            HAL_Delay(200);
            continue;
        }
        do {
            state = fpm383c_register_fingerprint_query(&id, &proc);
            HAL_Delay(200);
        } while (state == 0x04);
        switch (state) {
            case 0x08:display_error("no finger                 ");
            case 0x09:display_error("please again              ");
            case 0x10:display_error("small area! please again  ");
            case 0x11:display_error("move much! please again   ");
            case 0x12:display_error("move little! please again ");
            case 0x0e:display_error("poor quality! please again");
            case 0x0f:display_error("same! please again        ");
                continue;
            default:break;
        }
        display_progress(proc);
        if (proc >= 100){
            display_error("saving......        ");
            do {
                HAL_Delay(200);
                state = fpm383c_save_fingerprint(id);
            } while (state != 0x00);
            HAL_Delay(200);
            do {
                HAL_Delay(200);
                state = fpm383c_save_fingerprint_query();
                if (state == 0x00){
                    fpm383c_control_led(green);
                    oled_clear_buffer();
                    oled_update_screen();
                    return;
                }
           } while (state == 0x04);
        }
        else{
            uint8_t fingerState = 0;
            state = fpm383c_check_finger(&fingerState);
            while (state != 0x00 || fingerState == 0x01) {
                display_error("please again                   ");
                HAL_Delay(200);
                state = fpm383c_check_finger(&fingerState);
            }
            reg_idx ++;
        }
    }
}

uint16_t match_finger()
{
    uint8_t state;
    uint16_t id = 0xffff;
    uint8_t fingerState;
    fpm383c_check_finger(&fingerState);
    if (fingerState != 0x01){
//        display_error("no finger                 ");
        return 0xffff;
    }
    for (;;){
        if (fpm383c_match_fingerprint() == 0xff)
            return 0xffff;
        do {
            HAL_Delay(200);
            state = fpm383c_match_fingerprint_query(&id);
            if (state == 0x0e || state == 0x10 || state == 0x09)
                break;
            else if (state == 0x00 && id != 0xffff)
                return id;
            else if (state != 0x04 && state != 0x05)
                return 0xffff;
        } while (state == 0x04 || state == 0x05);
    }
}
